const Joi = require('joi');

async function listValidate(params) {
  const schema = Joi.object({
    parent_id: Joi.string().default('0'),
    pageIndex: Joi.string().default('1'),
    pageSize: Joi.string().default('10'),
  });

  try {
    const value = await schema.validateAsync(params);
    return value;
  } catch (error) {
    throw error;
  }
}

async function insertValidate(params) {

  const schema = Joi.object({
    name: Joi.string()
      .required()
      .pattern(/^[^\s]+$/)
      .messages({
        'string.empty': '名称不能为空',
        'string.pattern': '名称不能包含特殊字符',
        'any.required': '名称是必填项',
      }),
    key: Joi.string()
      .required()
      .pattern(/^[^\s]+$/)
      .messages({
        'string.empty': 'key不能为空',
        'string.pattern': '名称不能包含特殊字符',
        'any.required': 'key是必填项',
      }),
    value: Joi.number(),
    comment: Joi.string(),
    parent_id: Joi.string().default('0'),
    is_enable: Joi.number().default(0),
  });

  try {
    const value = await schema.validateAsync(params);
    return value;
  } catch (error) {
    throw error;
  }
}

async function updateValidate(params) {

  const schema = Joi.object({
    uuid: Joi.string().required().messages({
      'string.empty': 'uuid不能为空',
      'any.required': 'uuid是必填项',
    }),
    name: Joi.string(),
    key: Joi.string(),
    value: Joi.number(),
    comment: Joi.string(),
  });

  try {
    const value = await schema.validateAsync(params);
    return value;
  } catch (error) {
    throw error;
  }
}

async function enableValidate(params) {

  const schema = Joi.object({
    uuid: Joi.string().required().messages({
      'string.empty': 'uuid不能为空',
      'any.required': 'uuid是必填项',
    }),
    is_enable: Joi.string().required().messages({
      'string.empty': 'is_enable不能为空',
      'any.required': 'is_enable是必填项',
    }),
  });
  try {
    const value = await schema.validateAsync(params);
    return value;
  } catch (error) {
    throw error;
  }
}

async function moveValidate(params) {
  const schema = Joi.object({
    uuid: Joi.string().required().messages({
      'string.empty': 'uuid不能为空',
      'any.required': 'uuid是必填项',
    }),
    parent_id: Joi.string().required().messages({
      'string.empty': 'parent_id不能为空',
      'any.required': 'parent_id是必填项',
    }),
  });
  try {
    const value = await schema.validateAsync(params);
    return value;
  } catch (error) {
    throw error;
  }
}

async function deleteValidate(params) {
  const schema = Joi.object({
    ids: Joi.array().required().messages({
      'any.required': 'ids是必填项',
    }),
  });
  try {
    const value = await schema.validateAsync(params);
    return value;
  } catch (error) {
    throw error;
  }
}


module.exports = {
  listValidate,
  insertValidate,
  updateValidate,
  enableValidate,
  moveValidate,
  deleteValidate,
};
